package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Npc_Rush {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static boolean forNpcQuest(String s, L1PcInstance pc, L1NpcInstance npc, int npcid, int oid) {
    ArrayList aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      boolean success = false;
      if (aTempData.get(0) != null && ((Integer)aTempData.get(0)).intValue() == npcid && (
        (String)aTempData.get(1)).equals(s)) {
        int[] checkitem = (int[])aTempData.get(2);
        int chekitem_count = ((Integer)aTempData.get(3)).intValue();
        int chekitem_en = ((Integer)aTempData.get(4)).intValue();
        int newitem1 = ((Integer)aTempData.get(7)).intValue();
        int chekitem_en2 = ((Integer)aTempData.get(8)).intValue();
        int[] materials = (int[])aTempData.get(5);
        int[] counts = (int[])aTempData.get(6);
        if (materials != null) {
          int j = 0;
          while (j < materials.length) {
            if (!pc.getInventory().checkItem(materials[j], counts[j])) {
              L1Item temp = ItemTable.get().getTemplate(materials[j]);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp.getName())) + "(" + (
                    counts[j] - pc.getInventory().countItems(temp.getItemId())) + ")"));
              success = false;
            } 
            j++;
          } 
        } 
        int q = 0;
        while (q < checkitem.length) {
          if (pc.getInventory().checkEnchantItem(checkitem[q], chekitem_en, chekitem_count) && 
            pc.getInventory().checkItem(materials, counts)) {
            pc.getInventory().consumeEnchantItem(checkitem[q], chekitem_en, chekitem_count);
            int k = 0;
            while (k < materials.length) {
              pc.getInventory().consumeItem(materials[k], counts[k]);
              k++;
            } 
            L1ItemInstance item = ItemTable.get().createItem(newitem1);
            item.setEnchantLevel(chekitem_en2);
            item.setIdentified(true);
            pc.getInventory().storeItem(item);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(143, npc.getNpcTemplate().get_name(), item.getLogName()));
            success = true;
            pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
            break;
          } 
          q++;
        } 
        if (!success && aTempData.get(9) != null)
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), ""+aTempData.get(9))); 
      } 
      i++;
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_npc制作");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("npcid")));
          sTemp = rset.getString("action");
          aReturn.add(1, sTemp);
          aReturn.add(2, getArray(rset.getString("侦测身上物品"), ",", 1));
          aReturn.add(3, Integer.valueOf(rset.getInt("侦测身上物品数量")));
          aReturn.add(4, Integer.valueOf(rset.getInt("侦测物品加成")));
          aReturn.add(5, getArray(rset.getString("检查身上材料"), ",", 1));
          aReturn.add(6, getArray(rset.getString("检查身上材料数量"), ",", 1));
          aReturn.add(7, Integer.valueOf(rset.getInt("给予物品")));
          aReturn.add(8, Integer.valueOf(rset.getInt("给予物品加成")));
          aReturn.add(9, new String(rset.getString("不足显示对话")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
